# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        n1 = l1
        n2 = l2
        carry = 0
        while n1:
            n1.val = n1.val + n2.val + carry
            if n1.val > 9:
                carry = 1
                n1.val = n1.val - 10
            else:
                carry = 0
            if not n1.next:
                if carry:
                    n1.next = ListNode(0)
                else:
                    n1.next = n2.next
                    break
            n1 = n1.next
            if not n2.next:
                if carry:
                    n2.next = ListNode(0)
                else:
                    break
            n2 = n2.next

        return l1
